﻿// 注册ue4调用事件
if (typeof ue != "object") ue = {};
uuidv4 = function () {
    return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, function (c) {
        return (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16);
    });
};

ue4 = (function (register) {
    if (typeof ue.interface != "object" || typeof ue.interface.broadcast != "function") {
        // mobile
        ue.interface = {};
        return function (name, data, callback, timeout) {
            if (typeof name != "string")
                return;

            if (typeof data == "function") {
                timeout = callback;
                callback = data;
                data = null;
            }

            var uuid = register(callback, timeout);
            var args = [name, "", uuid];
            if (typeof data != "undefined")
                args[1] = data;

            var hash = encodeURIComponent(JSON.stringify(args));
            if (typeof history == "object" && typeof history.pushState == "function") {
                history.pushState({}, "", "#" + hash);
                history.pushState({}, "", "#" + encodeURIComponent("[]"));
            } else {
                document.location.hash = hash;
                document.location.hash = encodeURIComponent("[]");
            }
        };
    }

    return (function (interface) {
        // desktop
        ue.interface = {};
        return function (name, data, callback, timeout) {
            if (typeof name != "string")
                return;

            if (typeof data == "function") {
                timeout = callback;
                callback = data;
                data = null;
            }

            var uuid = register(callback, timeout);
            if (typeof data != "undefined")
                interface.broadcast(name, JSON.stringify(data), uuid);
            else
                interface.broadcast(name, "", uuid);
        };
    })
    (ue.interface);
})
(function (callback, timeout) {
    if (typeof callback != "function") return "";
    var uuid = uuidv4();
    ue.interface[uuid] = callback;
    setTimeout(function () {
            delete ue.interface[uuid];
        },
        1000 * Math.max(1, parseInt(timeout) || 0));

    return uuid;
});

function triggerUE4EventBlank() {
    var obj = getParameter((Array.prototype.slice.call(arguments, 0)));
    ue4(obj.functionsName, obj.args);
}

function controlPatternScene() {
    var obj = getParameter((Array.prototype.slice.call(arguments, 0)));
    ue4(obj.functionsName, obj.args);
}

function getParameter(parameterArray) {
    var obj = {
        functionsName: '123',
        id: '',
        className: '',
        value: '',
        args: ''
    };
    var tempArray = new Array();

    if (parameterArray.length > 0) {
        for (var i = 1; i <= parameterArray.length; i++) {
            if (typeof parameterArray[i] == 'object') {
                obj.id = parameterArray[i].id;
                obj.className = parameterArray[i].className;
                obj.value = parameterArray[i].value;
            } else {
                tempArray.push(parameterArray[i]);
            }
        }
    }
    obj.functionsName = parameterArray[0];
    obj.args = tempArray.join(';');
    return obj;
};

ue.interface.setFPS = function (fps) {
    alert(fps)
};